﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business
{
    /// <summary>
    /// 角色服务
    /// </summary>
    public class RoleService : IRoleService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Role model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(List<string> ids)
        {
            try
            {
                dal.BeginTran();

                bool result = dal.Updateable<T_Role>(new
                {
                    DataStatus = DataStatusEnum.Deleted,
                    ModifyTime = DateTime.Now,
                }).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;

                if (result == false)
                {
                    dal.RollbackTran();
                    return false;
                }

                result = dal.Delete<T_Role_Function>(p => ids.Contains(p.RoleID));

                if (result == false)
                {
                    dal.RollbackTran();
                    return false;
                }

                dal.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                dal.RollbackTran();
                throw ex;
            }
        }

        public T_Role GetById(string id)
        {
            return dal.Get<T_Role>(id);
        }

        public T_Role GetByRoleName(string roleName)
        {
            return dal.QueryOne<T_Role>(p => p.RoleName == roleName);
        }

        public List<T_Role> GetList(string companyId)
        {
            Expression<Func<T_Role, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid && a.RoleDescription != "超级管理员";
            if (!string.IsNullOrWhiteSpace(companyId))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyId, Expression.AndAlso);
            }
            return dal.Queryable<T_Role>().Where(whereExp).ToList();
        }

        public List<T_Role> GetList(List<string> ids)
        {
            return dal.Queryable<T_Role>().Where(p => p.DataStatus == DataStatusEnum.Valid && ids.Contains(p.ID)).ToList();
        }

        public PageData<T_Role> Query(string roleName, string companyId, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Role, bool>> whereExp = (a) => a.DataStatus == DataStatusEnum.Valid;
            if (!string.IsNullOrWhiteSpace(roleName))
            {
                whereExp = whereExp.Compose((a) => a.RoleName.StartsWith(roleName), Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(companyId))
            {
                whereExp = whereExp.Compose((a) => a.CompanyID == companyId, Expression.AndAlso);
            }

            List<T_Role> list = dal.Queryable<T_Role>().Where(whereExp).OrderBy(a => a.CreateTime, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Role> { Data = list, Page = page };
        }

        public bool Update(T_Role model)
        {
            return dal.Updateable<T_Role>(p => new T_Role()
            {
                RoleName = model.RoleName,
                RoleDescription = model.RoleDescription,
                ModifyUserId = model.ModifyUserId,
                ModifyUserName = model.ModifyUserName,
            }).Where(p => p.ID == model.ID).ExecuteCommand() > 0;
        }
    }
}
